'''
@author:gxj 2018/11
'''
from django.shortcuts import render,HttpResponse
from django.http import JsonResponse
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
from django.views import View

import json

booklist = []
for i in range(666):

    temp={'title':'root' + str(i), 'price': i}

    booklist.append(temp)

def index(request):
    ''' 自定义一分页组件'''
    print('---------------',request.GET.get('p'))
    per_page_count=10
    cuurent_page = request.GET.get('p')
    cuurent_page = int(cuurent_page)
    # print(cuurent_page)
    start = (cuurent_page - 1)*per_page_count
    end = cuurent_page*per_page_count
    count = len(booklist)
    # print(count)
    data = booklist[start:end]

    pre_page = cuurent_page-1
    next_page = cuurent_page+1

    if pre_page <=1:
        pre_page =1
    if next_page >= int(count/per_page_count):
        if count % per_page_count == 0:
            next_page = int(count/per_page_count)
        else:
            next_page = int(count/per_page_count)+1

    return render(request,"book.html",{'booklist':data,'pre_page':pre_page,'next_page':next_page})


class MySelfPaginator(Paginator):
    # 自定义类 继承django  Paginator元素类
    def __init__(self,current_page,per_pager_num,*args,**kwargs):
        self.current_page = int(current_page)
        # 当前页码数
        self.per_pager_num = int(per_pager_num)
        # 当前页最多显示页码数量 11
        super(MySelfPaginator,self).__init__(*args,**kwargs)
    def  pre_page_range(self):
        # self.current_page  # 当前页码数
        # self.per_pager_num #  当前页最多显示页码数量 11
        # self.paginator.num_pages # 页码总数
        # self.per_pager_num = int(per_pager_num)
        if self.paginator.num_pages < self.per_pager_num:
            return range(1,self.paginator.num_pages+1)
        part = int(self.per_pager_num/2)
        if self.current_page <= part:
            return range(1,self.num_pages+1)
        if (self.current_page + part) > self.paginator.num_pages:
            return range(self.num_pages - self.per_pager_num +1 , self.per_pager_num +1)
        return range(self.current_page-part,self.current_page+part+1)


def index02(request):
    ''' django框架 分页组件'''
    current_page = request.GET.get('p') # 当前页数
    # paginator = Paginator(booklist,10) # 获取数据 每页显示10条、django 自身的对象
    per_pager_num =11 # 当前页显示最大页面数
    current_page=int(current_page)
    paginator = MySelfPaginator(current_page,per_pager_num,booklist,10) # 获取数据 每页显示10条、自定义的类继承自身的对象、传入当前页、当前展示页码数量
    num_pages = paginator.num_pages # 获取总数数据

    part = int(per_pager_num/2)

    if paginator.num_pages < per_pager_num:
        pageRange = range(1,paginator.num_pages+1)
    elif current_page <= part:
        pageRange=range(current_page-5,paginator.num_pages+1)

    else:
        pageRange=range(current_page-5,current_page+5)

    try:
        post = paginator.page(current_page)
        # page 对象 获取当前页
    except EmptyPage:
        post = paginator.page(1)
    except PageNotAnInteger:
        post = paginator.page(paginator.num_pages)

    return render(request,'jingdong.html',{"post":post})

bookes = []
for i in range(666):

    temp={'title':'root' + str(i), 'price': i}

    bookes.append(temp)

from app02.pager import myPaginator
def index03(request):
    '''
    分页函数 当前页，url 总数为必传、每页显示数据数量、每页显示的页码数为非必传
    '''
    current_page = request.GET.get('p')
    url='/app02/book1.html'
    totalcount=80
    page_obj = myPaginator(url,totalcount,current_page,10,7)
    data = bookes[page_obj.start():page_obj.end()]
    return render(request,'book1.html',{'data':data,"page_obj":page_obj})